package com.autumn.mybatis.criterion.clauses;

import com.autumn.mybatis.criterion.CriteriaOperatorEnum;
import com.autumn.mybatis.criterion.LogicalOperatorEnum;
import com.autumn.util.StringUtils;

/**
 * 条件表达式
 * 
 * @author 老码农
 *
 *         2017-10-26 10:48:21
 */
public abstract class AbstractCriteriaClause extends AbstractClause {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5282864561043386524L;

	private LogicalOperatorEnum logical;
	private final CriteriaOperatorEnum operator;

	private String logic;
	private final String expression;
	private final String op;
	private Object value;
	private Object secondValue;
	private String leftBrackets;
	private String rigthBrackets;

	/**
	 * 
	 * @param logical
	 * @param expression
	 * @param operator
	 */
	public AbstractCriteriaClause(LogicalOperatorEnum logical, String expression, CriteriaOperatorEnum operator) {
		this.logical = logical;
		this.expression = expression;
		this.operator = operator;
		this.leftBrackets = "";
		this.rigthBrackets = "";
		this.logic = logical.getOperator();
		this.op = operator.getOperator();
	}

	/**
	 * 获取逻辑
	 * 
	 * @return
	 */
	public String getLogic() {
		return logic;
	}

	/**
	 * 获取操作符
	 * 
	 * @return
	 */
	public String getOp() {
		if (this.value == null) {
			return "IS";
		}
		return op;
	}

	/**
	 * 获取表达式(如列名称)
	 * 
	 * @return
	 */
	public String getExpression() {
		return this.expression;
	}

	/**
	 * 获取逻辑
	 * 
	 * @return
	 */
	public final LogicalOperatorEnum getLogical() {
		return logical;
	}

	/**
	 * 设置逻辑
	 * 
	 * @return
	 */
	public final void setLogical(LogicalOperatorEnum logical) {
		this.logical = logical;
		this.logic = logical.getOperator();
	}

	/**
	 * 获取运算符
	 * 
	 * @return
	 */
	public final CriteriaOperatorEnum getOperator() {
		return this.operator;
	}

	/**
	 * 生成表达式
	 * 
	 * @param paramName
	 *            参数名称
	 * @return
	 */
	public abstract String builderExpression(String paramName);

	/**
	 * 
	 * @return
	 */
	public final Object getValue() {
		return value;
	}

	public final void setValue(Object value) {
		this.value = value;
	}

	/**
	 * 获取 to Value
	 * 
	 * @return
	 */
	public final Object getSecondValue() {
		return secondValue;
	}

	/**
	 * 设置 to Value
	 * 
	 * @param secondValue
	 */
	public final void setSecondValue(Object secondValue) {
		this.secondValue = secondValue;
	}

	/**
	 * 添加左边括号
	 */
	public void addLeftBrackets() {
		if (StringUtils.isNullOrBlank(this.leftBrackets)) {
			this.leftBrackets = "(";
		} else {
			this.leftBrackets = this.leftBrackets + "(";
		}
	}

	/**
	 * 添加右边括号
	 */
	public void addRigthBrackets() {
		if (StringUtils.isNullOrBlank(this.rigthBrackets)) {
			this.rigthBrackets = ")";
		} else {
			this.rigthBrackets = this.rigthBrackets + ")";
		}
	}

	/**
	 * 获取左括号
	 * 
	 * @return
	 */
	public final String getLeftBrackets() {
		return leftBrackets;
	}

	/**
	 * 获取右括号
	 * 
	 * @return
	 */
	public final String getRigthBrackets() {
		return rigthBrackets;
	}
}
